#include "canvas.h"
#include "external/transformable/transformable.h"
#include "external/sync-layer/composer.h"
#include "external/paramo/paramo.h"
#include <QPainter>
#include <QPixmap>

Canvas::Canvas(QWidget *parent)
    : QWidget{parent}
{
    readConfig();

    // 创建时钟
    update_timer = new QTimer(this);
    connect(update_timer, &QTimer::timeout, this, [this]{ this->update(); });
    update_timer->start(update_interval);

    // 总是响应移动事件
    setMouseTracking(true);
}

Canvas::~Canvas()
{
    saveConfig();
}

void Canvas::mousePressEvent(QMouseEvent *event)
{
    // 获取修饰键
    auto modifiers = event->modifiers();

    // CTRL --> Transformable
    if (modifiers & Qt::ControlModifier)
    {
        TRANS->beginMove(event->position().x(), event->position().y());
    }
    else if (modifiers & Qt::AltModifier)
    {
        if (event->button() == Qt::LeftButton)
        {
            brush.begin(event->position().x(), event->position().y(), 1);
        }
        else if (event->button() == Qt::RightButton)
        {
            brush.begin(event->position().x(), event->position().y(), 0);
        }
    }
    else
    {
        ;
    }
}

void Canvas::mouseReleaseEvent(QMouseEvent *event)
{
    // 确保移动状态总是能释放掉
    TRANS->endMove(event->position().x(), event->position().y());
    brush.end(event->position().x(), event->position().y());
}

void Canvas::mouseMoveEvent(QMouseEvent *event)
{
    // Move事件总是传递给Transformable
    TRANS->handleMouseMove(event->position().x(), event->position().y());
    brush.move(event->position().x(), event->position().y());
}

void Canvas::wheelEvent(QWheelEvent *event)
{
    // 获取修饰键
    auto modifiers = event->modifiers();

    // CTRL --> Transformable
    if (modifiers & Qt::ControlModifier)
    {
        TRANS->doScale(event->position().x(), event->position().y(), event->angleDelta().y());
    }
}

void Canvas::resizeEvent(QResizeEvent *event)
{
    // Resize事件总是传递给Transformable
    TRANS->handleResize(event->size().width(), event->size().height());
}

void Canvas::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    // 更新图层
    COMPOSER->update(width(), height());

    // 创建空白画布
    QPixmap pixmap(width(), height());
    pixmap.fill(Qt::transparent);

    // 融合图层
    COMPOSER->compose(pixmap);

    // 绘制融合图层
    QPainter painter(this);
    painter.drawPixmap(0, 0, pixmap);
}

void Canvas::readConfig()
{
    // 读取时钟刷新间隔
    int interval;
    if (PARAMO->get("canvas.timer.interval", interval))
    {
        update_interval = interval;
    }
    else
    {
        // 写入默认值
        PARAMO->put("canvas.timer.interval", update_interval);
    }

    // 读取原点&缩放信息
    bool flag = true;
    double x, y, s;
    if (!PARAMO->get("canvas.base.x", x)) flag = false;
    if (!PARAMO->get("canvas.base.y", y)) flag = false;
    if (!PARAMO->get("canvas.base.s", s)) flag = false;

    // 读取成功，设置属性
    if (flag)
    {
        TRANS->setBase(x, y);
        TRANS->setScale(s);
    }
}

void Canvas::saveConfig()
{
    // 写入配置
    double x, y, s;
    TRANS->base(x, y);
    s = TRANS->scale();
    PARAMO->put("canvas.base.x", x);
    PARAMO->put("canvas.base.y", y);
    PARAMO->put("canvas.base.s", s);
}
